home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / udphdr.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  2KB  |  72 lines

  1. /* UDP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "config.h"
  6. #include "mbuf.h"
  7. #include "ip.h"
  8. #include "internet.h"
  9. #include "udp.h"
  10.  
  11. /* Convert UDP header in internal format to an mbuf in external format */
  12. struct mbuf *
  13. htonudp(udp,data,ph)
  14. struct udp *udp;
  15. struct mbuf *data;
  16. struct pseudo_header *ph;
  17. {
  18.     struct mbuf *bp;
  19.     register char *cp;
  20.     int16 checksum;
  21.  
  22.     /* Allocate UDP protocol header and fill it in */
  23.     if((bp = pushdown(data,UDPHDR)) == NULLBUF)
  24.         return NULLBUF;
  25.  
  26.     cp = bp->data;
  27.     cp = put16(cp,udp->source);    /* Source port */
  28.     cp = put16(cp,udp->dest);    /* Destination port */
  29.     cp = put16(cp,udp->length);    /* Length */
  30.     *cp++ = 0;            /* Clear checksum */
  31.     *cp-- = 0;
  32.  
  33.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  34.      * the spec requires us to change zeros into ones to distinguish an
  35.       * all-zero checksum from no checksum at all
  36.      */
  37.     if((checksum = cksum(ph,bp,ph->length)) == 0)
  38.         checksum = 0xffffffff;
  39.     put16(cp,checksum);
  40.     return bp;
  41. }
  42. /* Convert UDP header in mbuf to internal structure */
  43. int
  44. ntohudp(udp,bpp)
  45. struct udp *udp;
  46. struct mbuf **bpp;
  47. {
  48.     char udpbuf[UDPHDR];
  49.  
  50.     if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR)
  51.         return -1;
  52.     udp->source = get16(&udpbuf[0]);
  53.     udp->dest = get16(&udpbuf[2]);
  54.     udp->length = get16(&udpbuf[4]);
  55.     udp->checksum = get16(&udpbuf[6]);
  56.     return 0;
  57. }
  58. /* Extract UDP checksum value from a network-format header without
  59.  * disturbing the header
  60.  */
  61. int16
  62. udpcksum(bp)
  63. struct mbuf *bp;
  64. {
  65.     struct mbuf *dup;
  66.  
  67.     if(dup_p(&dup,bp,6,2) != 2)
  68.         return 0;
  69.     return pull16(&dup);
  70. }
  71.  
  72.